package com.alibaba.middleware.race.sync;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sync.mutiParse.ResultWrite;
import com.sync.utils.Message;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 *  
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
	private static final Logger logger = LoggerFactory.getLogger(Client.class);

	// 管道链路激活
	@Override
	public void channelActive(ChannelHandlerContext channel) throws Exception {
		super.channelActive(channel);
	}

	// 当服务器端返回应答消息时，channelRead方法被调用，Netty的ByteBuf中读取并打印应答消息
	@Override
	public void channelRead(ChannelHandlerContext channel, Object msg)
			throws Exception {
		Message message = (Message) msg;
		if (message.getStatus() == Message.ask) {
			logger.info("客户端接受到了数据");
			ResultWrite.writeToFile(message.getData());
			System.exit(-1);
		}

	}
	 
	@Override
	public void exceptionCaught(ChannelHandlerContext channel,
			Throwable cause) {
		// 释放资源
		channel.close();
	}
}
